<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$tabs=array(
	array('op'=>'list','title'=>'代理商等级管理'),
	array('op'=>'edit','title'=>'添加/编辑等级'),
);
set_html('tabs',$tabs);
$page_setting=$this->plugin_setting('page_setting');
if($_RQ['op']=='list'){	
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='level asc';
	$total='';
	$return['list']=$this->getall('level',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
		$item['conditions']=$page_setting['demolevel'];
	}
	//添加默认等级
	$default_level=$this->plugin_setting('default_level');
	$default_level=array(
		'id'=>'0',
		'title'=>$page_setting['demolevel'],
		'level'=>0,
		'conditions'=>$page_setting['demolevel'],
		'fee_rate1'=>$default_level['fee_rate1'],
		'fee_rate2'=>$default_level['fee_rate2'],
		'fee_rate3'=>$default_level['fee_rate3'],
		'fee_rate4'=>$default_level['fee_rate4'],
		'fee_rate5'=>$default_level['fee_rate5'],
	);
	array_unshift($return['list'],$default_level);
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'
		默认等级重命名：移步--》样式设置--》默认等级;
		<red>注意：可代理额不足的，将不能获得代理；
		</br>1、【培训奖】向下十代，见点奖励；触发条件，由基本设置里面的决定；这里只触发奖励额度！
		</br>2、【平超奖】向下十代，见点奖励；
		</br>3、【管理奖】每天营业额加权；
		</br>4、【级差奖】
		</br>5、拿着直推奖就没有培训奖；
		</br>6、拿着级差奖就没有平超奖；
		</red>
		','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string'),
			array('name'=>'fee_rate1','title'=>'推荐奖','type'=>'string'),
			array('name'=>'fee_rate2','title'=>'级差奖','type'=>'string'),
			array('name'=>'fee_rate3','title'=>'培训奖','type'=>'string'),
			array('name'=>'fee_rate4','title'=>'平超奖','type'=>'string'),
			array('name'=>'fee_rate5','title'=>'管理奖','type'=>'string'),
			array('name'=>'is_all5','title'=>'向下拿管理奖','type'=>'radio','options'=>'is'),
			array('name'=>'canfee4','title'=>'拿了级差奖也能拿平超奖','type'=>'radio','options'=>'is'),
		),'action'=>array(
			array('op'=>'look_condition','title'=>'升级条件','type'=>'modal','show'=>array('level >'=>0)),
			array('op'=>'edit','title'=>'编辑','type'=>'href','show'=>array('level >'=>0)),
			array('op'=>'set_default','title'=>'设置默认佣金','type'=>'href','show'=>array('level'=>0)),
		)),
	);
	$return['is']=array('否','是');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look_condition'){
	$item=$this->get('level',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']),array('conditions'));
	if(!$item){
		exi('指定等级不存在！','error');
	}
	
	$item['conditions']=json_decode($item['conditions'],true);
	$return['item']=$item;
	if(!$item['conditions']){
		exi('该等级尚未设置升级条件！','error');
	}
	$htmls=array(
		'type'=>'look',
		'look'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	foreach($current_level_up_types as $type){
		$htmls['look'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['conditions']=$item['conditions']?json_decode($item['conditions'],true):array('order_price'=>'0');
	}else{
		$item=array(
			'title'=>'',
			'conditions'=>array('mygxfee'=>'0'),
			'is_level_up_one'=>0,
		);
	}
	$item['goods_ids']=$item['goods_ids']?json_decode($item['goods_ids'],true):array();
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'等级设置',
		'edits'=>array(array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string','help'=>'填写一百以内的数字，数字越大表示等级越高！不同等级的等级权重不得相等！'),
		))),
	);
	//升级条件表单
	$condition_edit=array(
		'title'=>'升级条件',
		'list'=>array(),
	);
	$condition_edit['list'][]=array('type'=>'switch','title'=>'满足其中一个条件就升级','name'=>'is_level_up_one','help'=>'默认，勾选的条件都要满足才行！');
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model('model')->current_level_up_types();
	foreach($current_level_up_types as $type){
		if($type=='buy_goods'){
			$condition_edit['list'][]=array('show'=>array('is_level_up_one'=>0),'type'=>'choose_many','title'=>'购买指定商品升级','name'=>'goods_ids','action'=>'jy_weishop/goods.get_goods/many/','choose_title'=>'选择参与的商品','help'=>'（多个商品时，任意其中一个）注意：注意触发升级时机，需核销的商品，只有核销后才会完成！虚拟商品，发货后自动会完成！');
		}else{
			$condition_edit['list'][]=array('show'=>array('is_level_up_one'=>0),'type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
		}
	}
	$current_level_up_types=$this->model('model')->current_level_up_types(1);
	foreach($current_level_up_types as $type){
		if($type=='buy_goods'){
			$condition_edit['list'][]=array('show'=>array('is_level_up_one'=>1),'type'=>'choose_many','title'=>'购买指定商品升级','name'=>'goods_ids','action'=>'jy_weishop/goods.get_goods/many/','choose_title'=>'选择参与的商品','help'=>'（多个商品时，任意其中一个）注意：注意触发升级时机，需核销的商品，只有核销后才会完成！虚拟商品，发货后自动会完成！');
		}else{
			$condition_edit['list'][]=array('show'=>array('is_level_up_one'=>1),'type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
		}
	}
	$htmls['edits'][]=$condition_edit;
	$commission_edit=array(
		'title'=>'佣金设置',
		'list'=>array(
			array('name'=>'fee_rate1','title'=>'推荐奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('name'=>'fee_rate2','title'=>'级差奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('name'=>'fee_rate3','title'=>'培训奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('name'=>'fee_rate4','title'=>'平超奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('name'=>'canfee4','title'=>'拿了级差奖也能拿平超奖？','type'=>'switch','help'=>'最近的第一个不拿【平超奖】'),
			array('name'=>'fee_rate5','title'=>'管理奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('name'=>'is_all5','title'=>'向下拿管理奖','type'=>'switch'),
		),
	);
	$htmls['edits'][]=$commission_edit;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels'][0]=array(
		'id'=>'0',
		'title'=>$page_setting['demolevel'],
		'level'=>0,
	);
	$return['feetype']=$this->credit_type;
	$return['buy_creditfrom']=$this->buy_creditfrom;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'level'=>intval($postdata['level']),
		'conditions'=>$postdata['conditions']?ijson_encode($postdata['conditions']):'',
		'fee_rate1'=>$postdata['fee_rate1'],
		'fee_rate2'=>$postdata['fee_rate2'],
		'fee_rate3'=>$postdata['fee_rate3'],
		'fee_rate4'=>$postdata['fee_rate4'],
		'fee_rate5'=>$postdata['fee_rate5'],
		'is_all5'=>$postdata['is_all5'],
		'canfee4'=>$postdata['canfee4'],
		'is_level_up_one'=>intval($postdata['is_level_up_one']),
	);
	if($data['level']<=0 || $data['level']>100){
		exi('请填写正确的等级权重值，1-100','error');
	}
	if(!$data['title']){
		exi('请填写标题','error');
	}	
	$id=intval($postdata['id']);
	if($id){
		$this->update('level',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$this->insert('level',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set_default'){
	//设置默认佣金比例
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->plugin_setting('default_level',$post);
		exi('默认等级佣金设置成功！','','list');
	}
	$item=$this->plugin_setting('default_level');
	if(!$item){
		$item=array('ji_rate'=>'10%');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置默认佣金',
		'edit'=>array(
			array('name'=>'fee_rate1','title'=>'推荐奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('name'=>'fee_rate2','title'=>'级差奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('name'=>'fee_rate3','title'=>'培训奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('name'=>'fee_rate4','title'=>'平超奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
			array('name'=>'fee_rate5','title'=>'管理奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
		),
	);
	$return['feetype']=$this->credit_type;
	$return['buy_creditfrom']=$this->buy_creditfrom;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		$default_level=$this->plugin_setting('default_level');
		//exi('参数错误','error');
	}
	$post=get_postdata();
	if(isset($post['is_day'])){
		$update=array('is_day'=>$post['is_day']);
		if(!$id){
			$default_level['is_day']=$post['is_day'];
		}
	}
	if($update){
		if(!$id){
			$this->plugin_setting('default_level',$default_level);
		}else{
			$this->update('goods',$update,array('id'=>$id));
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_baojdai_level',array('id'=>$id));	
	}
	if(!$id){
		exi('默认等级不能删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_baojdai_level',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>